﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PetaPoco
{
    public class ConfigConstant
    {
        #region Const
        
        /// <summary>
        /// 只读数据库连接标识
        /// </summary>
        public const string READER_CONNECTION_NAME = "ReaderDb";

        /// <summary>
        /// 只写数据库连接标识
        /// </summary>
        public const string WRITE_CONNECTION_NAME = "WriteDb";

        #region TempDb
        /// <summary>
        /// 临时数据库连接标识
        /// </summary>
        public const string TEMP_CONNECTION_NAME = "TempDb";
        #endregion

        #endregion

        /// <summary>
        /// 重新上行次数
        /// </summary>
        public static int GetReupload
        {
            get
            {
                int reuploadCount = int.Parse(ConfigHelper.GetAppSetting("Reupload"));
                return reuploadCount;
            }
        }

        public static bool IsMainCenterServer
        {
            get { return bool.Parse(ConfigHelper.GetAppSetting("main_center_server")); ;}
        }

        #region Member Variables
        private static string _dbType = null;
        private static string _readerConnectionString = null;
        private static string _writeConnectionString = null;
        #endregion


        private ConfigConstant()
        { 
        }


        #region Properties
        
        /// <summary>
        /// 读取数据库连接字符串
        /// </summary>
        public static string ReaderConnectionString
        {
            get
            {
                if (_readerConnectionString == null)
                {
                    _readerConnectionString = ConfigHelper.GetConnectionString(READER_CONNECTION_NAME);
                }
                return _readerConnectionString;
            }
        }

        /// <summary>
        /// 写入数据库连接字符串
        /// </summary>
        public static string WriteConnectionString
        {
            get
            {
                if (_writeConnectionString == null)
                {
                    _writeConnectionString = ConfigHelper.GetConnectionString(WRITE_CONNECTION_NAME);
                }
                return _writeConnectionString;
            }
        }

        #endregion
    }
    #region 获得config的东西
    public static class ConfigHelper
    {
        public static string GetAppSetting(string key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[key];
        }

        public static string GetConnectionString(string key)
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings[key].ConnectionString;
        }
    }
    #endregion
}
